﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Transactions;
using Components;
using Components.Enum;
using Model;
using MsDAL;
using System.ComponentModel;
using ShopSetting = ServiceHelper.Set.ShopSetting;

namespace ServiceHelper
{
    [DataObjectAttribute]
    public partial class Ord3a00Manager : Ord3a00Base
    {
        private Ord3a00DAL ord3a00Dal = new Ord3a00DAL();
        private static Ord3a00Manager _entity;

        public static Ord3a00Manager Entity
        {
            get
            {
                if (_entity == null)
                    _entity = new Ord3a00Manager();
                return _entity;
            }
        }

        #region 个人代码

        public string SaveData(Hashtable tb, DataTable dtl, DataTable deldata, string action, string usercode)
        {
            List<SqlApp> lst = new List<SqlApp>();

            Ord3a00 model = null;
            Ord3a01 dtlModel = null;
            if ("A" == action && (dtl == null || dtl.Rows.Count == 0)) return "明细中没有数据，不需要保存！";

            string appstr = Context.GetAppByUserCode(usercode);

            ShopSetting shopSetting = null;

            using (var tran = new TransactionScope())
            {
                if ("A" == action) //新增
                {
                    //保存概要表身
                    model = new Ord3a00();
                    Components.CommonUtil.GetInstance.CopyProperties(tb, model);

                    shopSetting = new ShopSetting(model.Splistid);

                    model.Docno = Sys0a60Manager.Entity.CreateSn("ord3a00_docno", usercode);
                    model.Listid = Sys0a60Manager.Entity.CreateSn("ord3a00_listid", usercode);
                    model.Muser = appstr;
                    model.Utext1 = shopSetting.ParseJson();

                    if (!shopSetting.DirectStock)
                    {
                        if (shopSetting.NeedAudit == false)
                            model.Audstatus = Validation.ToInt(AuditStatus.NoRequirement);
                    }
                    else
                    {
                        model.Canedit = StringBool.False;
                    }

                    lst.Add(this.Add(model));

                    foreach (DataRow dr in dtl.Rows)
                    {
                        dtlModel = new Ord3a01();
                        Components.CommonUtil.GetInstance.CopyProperties(dtlModel, dr);
                        dtlModel.Listid =
                            Components.Validation.ToInt(Sys0a60Manager.Entity.CreateSn("ord3a01_listid", usercode));
                        dtlModel.Plistid = model.Listid;

                        lst.Add(Ord3a01Manager.Entity.Add(dtlModel));

                        if (shopSetting.DirectStock)
                        {
                            //直接发货
                            lst.Add(ord3a00Dal.Send(
                                Validation.ToString(dtlModel.Listid),
                                dtlModel.Ordqty,
                                dtlModel.Preqty)
                                );
                        }
                    }

                    if (shopSetting.DirectStock)
                    {
                        //更新订单状态(将订单状态更新为配货状态)
                        lst.Add(ord3a00Dal.AlreadySendNonePrint(model.Listid));
                    }
                }
                else if ("M" == action)
                {
                    model = this.GetModelById(Components.Validation.ToString(tb["listid"]));
                    if (null == model) return "该笔订单可能已经被删除！";
                    Components.CommonUtil.GetInstance.CopyProperties(tb, model);

                    lst.Add(this.Update(model));

                    foreach (DataRow dr in dtl.Rows)
                    {
                        dtlModel = new Ord3a01();
                        Components.CommonUtil.GetInstance.CopyProperties(dtlModel, dr);

                        if (!Components.Validation.IsNull(dr["listid"]))
                        {
                            lst.Add(Ord3a01Manager.Entity.Modify(dtlModel));
                        }
                        else
                        {
                            dtlModel.Listid =
                                Components.Validation.ToInt(Sys0a60Manager.Entity.CreateSn("ord3a01_listid", usercode));
                            dtlModel.Plistid = model.Listid;

                            lst.Add(Ord3a01Manager.Entity.Add(dtlModel));
                        }
                    }

                    if (null != deldata)
                    {
                        foreach (DataRow dr in deldata.Rows)
                        {
                            dtlModel = new Ord3a01();
                            dtlModel.Listid = Components.Validation.ToInt(dr["listid"]);
                            if (dtlModel.Listid == 0) continue;

                            lst.Add(Ord3a01Manager.Entity.Delete(dtlModel));
                        }
                    }
                }
                else if ("D" == action) //删除
                {
                    model = this.GetModelById(Components.Validation.ToString(tb["listid"]));
                    if (null == model) return "该笔订单可能已经被删除！";

                    if (model.Status != 1)
                    {
                        return "只有正常的订单才能被删除";
                    }

                    model.Status = 9; //删除
                    lst.Add(this.Update(model));
                }
                else if ("P" == action) //审核
                {
                    //2011-03-24 加入更新仓库
                    lst.Add(this.Audit(Components.Validation.ToString(tb["listid"]),
                                       Components.Validation.ToString(tb["storageid"])));
                }

                var msg = CommonManager.Entity.SaveData(lst);

                if (string.IsNullOrEmpty(msg)) //success
                {
                    tran.Complete();

                    if (null == model)
                        return null;

                    return Json.ToString(new {status = true, id = model.Listid});
                }

                return msg;
            }
        }

        public SqlApp Audit(string listid, string storageid)
        {
            return ord3a00Dal.Audit(listid, storageid);
        }

        public SqlApp AlreadyTackTicket(string listid)
        {
            return ord3a00Dal.AlreadyTackTicket(listid);
        }

        public SqlApp DoAdjust(string rowId, double qty, double amt)
        {
            return ord3a00Dal.DoAdjust(rowId, qty, amt);
        }

        public SqlApp UpdateSendAmt(string listid)
        {
            return ord3a00Dal.UpdateSendAmt(listid);
        }

        #endregion
    }
}



